package com.canplay.screen.adapter

import android.content.Context
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.canplay.screen.R
import com.canplay.screen.db.DataItem

class MyAdapter(private val items: List<DataItem>) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {

    var context : Context? = null;
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val name: TextView = itemView.findViewById(R.id.tx_item_name)
        val unit: TextView = itemView.findViewById(R.id.tx_item_unit)
        val price: TextView = itemView.findViewById(R.id.tx_item_price)
        val remark: TextView = itemView.findViewById(R.id.tx_item_remark)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.list_item_layout, parent, false)
        context = parent.context
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items[position]
        holder.name.text = item.projectName
        holder.unit.text = item.unit
        holder.price.text = item.projectPrice
        holder.remark.text = item.remark
        val typeface = Typeface.createFromAsset(context?.assets, "smqfont.ttf")
        holder.name.typeface = typeface
        holder.remark.typeface = typeface
    }

    override fun getItemCount(): Int {
        return items.size
    }
}